import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:yp/helper.dart';
import 'package:yp/router.dart';

import 'logic.dart';
import 'state.dart';

class InvoiceHeaderManagementPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _InvoiceHeaderManagementPage();
  }
}

class _InvoiceHeaderManagementPage extends State<InvoiceHeaderManagementPage> {
  final InvoiceHeaderManagementLogic logic =
      Get.put(InvoiceHeaderManagementLogic());
  final InvoiceHeaderManagementState state =
      Get.find<InvoiceHeaderManagementLogic>().state;

  bool isSubmit = false;
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        elevation: setWidth(1),
        backgroundColor: Colors.white,
        title: Text(
          '发票抬头管理',
          style: TextStyle(
            color: Color(0xff020202),
            fontSize: 17,
            fontWeight: FontWeight.bold,
          ),
        ),
        iconTheme: IconThemeData(
          color: Color(0xff020202),
        ),
      ),
      body: GetBuilder<InvoiceHeaderManagementLogic>(
        builder: (logic) {
          return Stack(
            children: [
              Container(
                color: Color(0xfff5f5f5),
                child: ListView.separated(
                  scrollDirection: Axis.vertical,
                  padding: EdgeInsets.only(
                    left: setWidth(20),
                    top: setWidth(40),
                    right: setWidth(20),
                  ),
                  itemBuilder: (context, index) {
                    Map<String, dynamic> map = state.commonInvoiceHeader[index];
                    return Container(
                      height: setWidth(162),
                      decoration: BoxDecoration(
                        color: Colors.white,
                        borderRadius: BorderRadius.circular(setWidth(10)),
                      ),
                      padding: EdgeInsets.symmetric(horizontal: setWidth(44)),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          InkWell(
                            onTap: () {
                              print('发票抬头列表 点击事件！！！ index = $index');
                              if (state.chooseBool) {
                                Get.back(result: map);
                              } else {
                                Get.toNamed(RouteConfig.addInvoiceHead,
                                        parameters: {'indexType': '2'},
                                        arguments: map)
                                    .then((value) {
                                  logic.onReady();
                                });
                              }
                            },
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              mainAxisAlignment: MainAxisAlignment.center,
                              children: [
                                Text(
                                  map['invoice_title'],
                                  maxLines: 1,
                                  overflow: TextOverflow.ellipsis,
                                  style: TextStyle(
                                    color: Color(0xff373737),
                                    fontSize: 17,
                                    fontWeight: FontWeight.bold,
                                  ),
                                ),
                                SizedBox(height: setWidth(4)),
                                Text(
                                  (map['invoice_type']?.toString() ?? '') == '1'
                                      ? '个人'
                                      : '企业',
                                  style: TextStyle(
                                    color: Color(0xff717171),
                                    fontSize: 14,
                                  ),
                                ),
                              ],
                            ),
                          ),
                          GestureDetector(
                            onTap: () {
                              showDialog(
                                context: context,
                                builder: (BuildContext context) {
                                  return CupertinoAlertDialog(
                                    title: Text("提示"),
                                    content: Column(
                                      children: <Widget>[
                                        SizedBox(
                                          height: 10,
                                        ),
                                        Align(
                                          child: Text("是否删除发票抬头"),
                                          alignment: Alignment(0, 0),
                                        ),
                                      ],
                                    ),
                                    actions: <Widget>[
                                      CupertinoDialogAction(
                                        child: Text("取消"),
                                        onPressed: () {
                                          Navigator.pop(context);
                                          print("取消");
                                        },
                                      ),
                                      CupertinoDialogAction(
                                        child: Text("确定"),
                                        onPressed: () {
                                          logic.deleteInvoiceHeader(index);
                                          Navigator.pop(context);
                                        },
                                      ),
                                    ],
                                  );
                                },
                              );
                            },
                            child: Icon(Icons.delete),
                          )
                        ],
                      ),
                    );
                  },
                  separatorBuilder: (context, index) {
                    return SizedBox(height: setWidth(20));
                  },
                  itemCount: state.commonInvoiceHeader.length,
                  // shrinkWrap: true,
                  // physics: NeverScrollableScrollPhysics(),
                ),
              ),
              Positioned(
                left: setWidth(20),
                right: setWidth(20),
                bottom: setWidth(86),
                child: InkWell(
                  onTap: () {
                    print('========= 添加发票抬头 点击事件 =========');

                    Get.toNamed(RouteConfig.addInvoiceHead,
                        parameters: {'indexType': '1'}).then((value) {
                      logic.onReady();
                    });
                  },
                  child: Container(
                    height: setWidth(88),
                    decoration: BoxDecoration(
                      color: Color(0xffea5504),
                      borderRadius: BorderRadius.circular(setWidth(10)),
                    ),
                    alignment: Alignment.center,
                    child: Text(
                      '添加发票抬头',
                      style: TextStyle(
                        color: Colors.white,
                        fontSize: 17,
                      ),
                    ),
                  ),
                ),
              ),
            ],
          );
        },
      ),
    );
  }
}
